package com.shop2cn.iapi.member.testcase;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.member.parameter.CreateMemberBean;
import com.shop2cn.iapi.member.parameter.ExportMemberInfoBean;
import com.shop2cn.iapi.member.service.ExportMemberInfoCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.GodMemberService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.sqlwapper.godmember.GodMemberWapper;
import com.ymttest.datadriver.ExcelOperate;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.ZipUtils;

/**
 * 导出报表文件 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ExportMemberInfo {
	private static ExportMemberInfoBean exportmemberinfoBean;
	private static ExportMemberInfoCall exportmemberinfoCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("导出报表文件");
	}

	@Before
	public void caseUp() {
		exportmemberinfoBean = new ExportMemberInfoBean();
		exportmemberinfoCall = new ExportMemberInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(exportmemberinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ExportMemberInfo_001() {
		Logger.start(true, "导出报表文件成功");
		try {
			TestDataManager.removeTempFile();
			Integer shopid = GodmchManageCallService.CreateSellerMch();
			// 开启邀请制
			GodMemberService.setCollectionMobile(shopid, true);
			GodMemberService.setInvitationCodeCall(shopid, true, 0);
			// 创建2个客服
			UserCreateBean u = DGUserCallService.initCsUsercreateBean();
			Integer csId = DGUserCallService.UserCreateCall(u);
			UserCreateBean u2 = DGUserCallService.initCsUsercreateBean();
			Integer csId2 = DGUserCallService.UserCreateCall(u2);
			// 创建一个新会员
			for (int i = 0; i < 1; i++) {
				UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
				int userId = DGUserCallService.UserCreateCall(usercreateBean);
				CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid, userId,
						usercreateBean.getOpenId());
				createMem.setOpenId(usercreateBean.getOpenId());
				String memid = GodMemberService.creatememberCall(createMem);
				String sqOrderRmbAmount = "101";
				Long orderid = Long.valueOf(TestDataManager.getRandomNum(10));
				GodMemberService.processOrderPay(userId, shopid, orderid, sqOrderRmbAmount);
				String mobile = TestDataManager.getPhoneNum();
				GodMemberService.bindMobile(shopid, userId, mobile, csId);
			}
//			GodMemberWapper GodMemberWapper=new GodMemberWapper();
//			Logger.verifyEquals(csId, GodMemberWapper.selectMemberByMemberid(memid).get(0).getCsId(), "验证csId");

			exportmemberinfoBean.setShopId(shopid);
			exportmemberinfoBean.setPageIndex(1);
			exportmemberinfoBean.setPageSize(1000);
			exportmemberinfoBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(0, "yyyy-MM-dd"));
			exportmemberinfoBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(0, "yyyy-MM-dd 23:59:59"));
			exportmemberinfoCall.setData(exportmemberinfoBean);
			exportmemberinfoCall.callService();
			Logger.verifyEquals("200", exportmemberinfoCall.getString("code"), "验证code");
			String url = exportmemberinfoCall.getString("data");
			String file = TestDataManager.DownloadFile(url);
			String savefile = System.getProperty("user.dir") + File.separator + "temp" + File.separator;
			ZipUtils.unZip(file, savefile);
			TestDataManager.getFilelist(savefile).forEach(x -> {
				if (x.endsWith("xlsx")) {
					List<Map<String, String>> mapList;
					try {
						mapList = ExcelOperate.getXLSXDataAsMap(new File(x), 0);
						System.out.println(mapList);
						// [{用户编号=501071365, 注册时间=2020/6/11 10:06:20, 消费金额=101, 用户昵称=NickautoTest,
						// 客服编号=501071363, 微信号=, 手机号=13250791854, 消费次数=1, 会员编号=HY20200611005443}]
						List<Map<String, String>> mapList2 = ExcelOperate.getXLSXDataAsMap(new File(x), 1);
						// [{客服编号=501071363, 日期=2020/6/11, 注册量=1}]
						System.out.println(mapList2);
					} catch (InvalidFormatException e) {
						e.printStackTrace();
					}
				}
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}